package cn.biplam.front.template.control.table;

import cn.biplam.common.tools.TOOLS;
import cn.biplam.common.wrapper.ErrorMessage;
import cn.biplam.common.wrapper.KvMap;
import cn.biplam.front.htmlscript.script.AjxScript;
import cn.biplam.front.htmlscript.script.AjxScript.Is_SubMitData;
import cn.biplam.front.htmlscript.script.AjxScript.Level;
import cn.biplam.front.template.control.rfFlowTable.callback.action.ActionCallback;

/**
 * 功能	表格基本属性类
 * @author XiaoRuxing
 * @create 2009-5-4下午03:56:25
 * @version 1.0
 */
public class TableScript {

	/** 表格操作类型 */
	public enum Action_Type{

		/** 提交刷新目标区域 */
		NONE_POPE_M,
		/** 有数据提交，无弹出窗口动作 */
		NONE_POPE_SUBMIT,
		/** 有数据提交有弹出窗口动作P */
		POP_SUBMIT_P,
		/** 有数据提交有弹出窗口动作Pr */
		POP_SUBMIT_PR,
		/** 弹出窗口内的数据提交/同步 */
		SP,
		/** 弹出窗口内的数据提交刷新目标区域 */
		SP_REFRESH_PARENT,
		/** 有数据提交有弹出窗口动作Pv */
		POP_SUBMIT_PV
	}

	/**
	 *  根据动作回调器生成提交脚本(用于反射FlowTable的单元格或者按钮动作脚本生成)
	 * @param action           动作回调器
	 * @param submitContainer  提交容器
	 * @param recordId         行记录ID
	 * @return 当前动作回调器对应的提交脚本
	 * @throws ErrorMessage
	 */
	public static String getBindScript(ActionCallback action,String submitContainer,String recordId) throws ErrorMessage{
		
		Action_Type actionType = action.getActionType();//动作类型
		String actionID = action.getActionId();//动作ID
		String targetcId = action.getTargetId();//目标容器ID
		KvMap<String, String> params = null;//附加参数
		if(action.getAddParams()!=null){
			params = AjxScript.OtherParamsToKvMap(action.getAddParams());//将变参转换为KvMap
		}
		
		switch(actionType){
		case NONE_POPE_M:
			return AjxScript.getM(submitContainer, actionID, recordId, targetcId);
		case NONE_POPE_SUBMIT:
			return AjxScript.getA(submitContainer,actionID);
		case POP_SUBMIT_P:	
			return AjxScript.getP(submitContainer, actionID,getLevel(targetcId),Is_SubMitData.Submit);
		case POP_SUBMIT_PR:
			return AjxScript.getPr(submitContainer, actionID,Is_SubMitData.Submit, action.getOtherContainer());
		case SP:
			params.put(".rid", recordId);
			/**Sp返回脚本刷新*/
			return AjxScript.getSpParamsNoRefresh(submitContainer, actionID,params);
		case SP_REFRESH_PARENT:
			/**Sp返回容器刷新*/
			return AjxScript.getSpParams(submitContainer, actionID, targetcId, recordId,params);
		case POP_SUBMIT_PV:
			return AjxScript.getPv(submitContainer, actionID,Is_SubMitData.Submit, null); 
		default :
			return null;
		}
	}
	
	/**
	 * 功能 获取表格绑定脚本
	 * @param actionType 脚本函数
	 * @param oID        容器ID
	 * @param actID      动作ID
	 * @param TargetID   目标ID
	 * @return 单元格绑定脚本
	 * @throws ErrorMessage
	 */
	public static String getBindScript(Action_Type actionType, String oID,
			String actID, String TargetID, 
			String recordId,String... otherParams) throws ErrorMessage {
		KvMap<String, String> params;
		switch(actionType){
		case NONE_POPE_M:
			return AjxScript.getM(oID, actID, recordId, TargetID);
		case NONE_POPE_SUBMIT:
			return AjxScript.getA(oID,actID);
		case POP_SUBMIT_P:	
			return AjxScript.getP(oID, actID,getLevel(TargetID),Is_SubMitData.Submit);
		case POP_SUBMIT_PR:
			String otherContainer = null;
			if(otherParams!=null && otherParams.length>0){
				otherContainer = otherParams[0];
			}
			return AjxScript.getPr(oID, actID,Is_SubMitData.Submit, otherContainer);
			
		case SP:
			//将变参转换为KvMap
			params = AjxScript.OtherParamsToKvMap(otherParams);
			params.put(".rid", recordId);
			/**Sp返回脚本刷新*/
			return AjxScript.getSpParamsNoRefresh(oID, actID,params);
		case SP_REFRESH_PARENT:
			//将变参转换为KvMap
			params= AjxScript.OtherParamsToKvMap(otherParams);
			/**Sp返回容器刷新*/
			return AjxScript.getSpParams(oID, actID, TargetID, recordId,params);

		case POP_SUBMIT_PV:
			return AjxScript.getPv(oID, actID,Is_SubMitData.Submit, null); 
		default :
			return null;
		}
	}

	/**
	 *  获取P弹出窗口级别
	 */
	private static Level getLevel(String level){
		switch(TOOLS.stringToInt(level)){
		case 1:
			return Level.First;
		case 2:
			return Level.Second;
		case 3:
			return Level.Third;
		case 4:
			return Level.Fourth;
		case 5:
			return Level.Fifth;
		case 6:
			return Level.Sixth;
		case 7:	
			return Level.Seventh;
		default:
			return null;	
		}
	}
}
